package fr.cnam.ratpProject.server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import fr.cnam.ratpProject.client.services.LigneService;
import fr.cnam.ratpProject.sharedDatas.Ligne;
import fr.cnam.ratpProject.sharedDatas.Station;

@SuppressWarnings("serial")
public class LigneServiceImpl extends AbstractServiceImpl implements LigneService{

	@Override
	public Ligne getLigne(String idLigne) {
		Statement stmt1 = jdbc.getStatement();
		String sql = "Select * from Ligne WHERE Ligne.idLigne =" + idLigne + ";" ;
		Ligne ligne = new Ligne();
		ResultSet rs, resultLigne;
		try {
			rs = stmt1.executeQuery(sql);
			ligne.setIdLigne(idLigne);
			ligne.setNom(rs.getString("nomLigne"));
			ligne.setCouleur(rs.getString("codeCouleur"));
			ligne.setUrlImage(rs.getString("urlLogo"));

			Statement stmt2 = jdbc.getStatement();
			String sql2 = "SELECT S.nomStation FROM LigneStation LS, Station S WHERE LS.idStation = S.idStation AND LS.idLigne = " + idLigne  +" ORDER BY LS.ordre;";
			resultLigne = stmt2.executeQuery(sql2);
			ArrayList<Station> lstStation = new ArrayList<Station>(); 
			while(resultLigne.next()){
				Station station = new Station();
				station.setName(resultLigne.getString("nomStation"));
				station.setLongitude("longitude");
				station.setLatitude("latitude");
				lstStation.add(station);
			}
			ligne.setDirection(lstStation.get(lstStation.size()));
			ligne.setLigne(lstStation);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ligne;
	}
	
	

}
